package com.github.fujianlian.klinechart.draw;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.github.fujianlian.klinechart.BaseKLineChartView;
import com.github.fujianlian.klinechart.R;
import com.github.fujianlian.klinechart.base.IValueFormatter;
import com.github.fujianlian.klinechart.entity.h.ICCI;
import com.github.fujianlian.klinechart.formatter.ValueFormatter;
import com.github.fujianlian.klinechart.utils.ViewUtil;

import java.util.List;

/**
 * KDJ实现类
 * Created by tifezh on 2016/6/19.
 */

public class CCIDraw extends BaseDraw<ICCI,ICCI> {
    private Paint mCCIPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mLine1Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mLine2Paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    float lineTop = 100;
    float lineBottom = -100;

    public CCIDraw() {
        super(ICCI.class);
    }

    public CCIDraw(BaseKLineChartView view, float lineTop, float lineBottom) {
        this(view);
        this.lineTop = lineTop;
        this.lineBottom = lineBottom;
    }

    public CCIDraw(BaseKLineChartView view) {
        super(ICCI.class);

        init(view);
    }

    @Override
    public void init(BaseKLineChartView view) {
        mCCIPaint.setColor(ContextCompat.getColor(view.getContext(), R.color.chart_blick));
        mCCIPaint.setTextSize(ViewUtil.Dp2Px(view.getContext(), 10));


        mLine1Paint.setColor(ContextCompat.getColor(view.getContext(), R.color.chart_red));
        mLine2Paint.setColor(ContextCompat.getColor(view.getContext(), R.color.chart_green));

        mLine1Paint.setStrokeWidth(3);
        mLine2Paint.setStrokeWidth(3);
    }

    public float getLineTop() {
        return lineTop;
    }

    public void setLineTop(float lineTop) {
        this.lineTop = lineTop;
    }

    public float getLineBottom() {
        return lineBottom;
    }

    public void setLineBottom(float lineBottom) {
        this.lineBottom = lineBottom;
    }


    @Override
    public void drawTranslated(@Nullable ICCI lastPoint, @NonNull ICCI curPoint, float lastX, float curX, @NonNull Canvas canvas, @NonNull BaseKLineChartView view, int position) {
//        if (!lastPoint.getCCI().isNaN() && !view.isLine()) {
//            view.drawVolLine(this, canvas, mCCIPaint, lastX, lastPoint.getCCI(), curX, curPoint.getCCI());
//        }
//
//        Rect chartRect = view.getChartRect(this);
//        if (view.getVolY(this, 100) >= chartRect.top) {
//            view.drawVolLine(this, canvas, mLine1Paint, lastX, 100, curX, 100);
//        }
//
//        if (view.getVolY(this, -100) <= chartRect.bottom) {
//            view.drawVolLine(this, canvas, mLine2Paint, lastX, -100, curX, -100);
//        }
    }

    @Override
    public void drawText(@NonNull Canvas canvas, @NonNull BaseKLineChartView view, int position, float x, float y) {
        ICCI point = (ICCI) view.getItem(position);
        String text = "CCI(14)  ";
        canvas.drawText(text, x, y, view.getTextPaint());
        x += view.getTextPaint().measureText(text);
        text = "" + view.formatValue(point.getCCI()) + " ";
        canvas.drawText(text, x, y, mCCIPaint);
    }

    @Override
    public Float getMaxValue(ICCI point) {
        return point.getCCI();
    }

    @Override
    public float getMinValue(ICCI point) {
        return point.getCCI();
    }

    @Override
    public IValueFormatter getValueFormatter() {
        return new ValueFormatter();
    }

    @Override
    public void drawTranslateds(Canvas canvas, BaseKLineChartView view, List<ICCI> datas, int mStartIndex, int mStopIndex) {

        Rect chartRect = view.getChartRect(this);

        float startX = view.getX(mStartIndex);
        float endX = view.getX(mStopIndex);

        float volY1 = view.getVolY(this, lineTop);
        if (volY1 >= chartRect.top) {
            canvas.drawLine(startX, volY1, endX, volY1, mLine1Paint);
        }

        float volY = view.getVolY(this, lineBottom);
        if (volY <= chartRect.bottom) {
            canvas.drawLine(startX, volY, endX, volY, mLine2Paint);
        }

        float[] pts = new float[(mStopIndex - mStartIndex + 1) * 4];

        for (int i = mStartIndex; i <= mStopIndex; i++) {
            ICCI currentPoint = (ICCI) view.getItem(i);
            float currentPointX = view.getX(i);

            ICCI lastPoint = i == 0 ? currentPoint : (ICCI) view.getItem(i - 1);
            float lastX = i == 0 ? currentPointX : view.getX(i - 1);

            pts[(i - mStartIndex) * 4] = lastX;
            pts[(i - mStartIndex) * 4 + 1] = view.getVolY(this, lastPoint.getCCI());
            pts[(i - mStartIndex) * 4 + 2] = currentPointX;
            pts[(i - mStartIndex) * 4 + 3] = view.getVolY(this, currentPoint.getCCI());

        }


        view.drawLines(canvas, mCCIPaint, pts);

    }

    /**
     * 设置K颜色
     */
    public void setKColor(int color) {
        mCCIPaint.setColor(color);
    }

    /**
     * 设置D颜色
     */
    public void setDColor(int color) {
    }

    /**
     * 设置J颜色
     */
    public void setJColor(int color) {
    }

    /**
     * 设置曲线宽度
     */
    public void setLineWidth(float width) {
        mCCIPaint.setStrokeWidth(width);

    }

    /**
     * 设置文字大小
     */
    public void setTextSize(float textSize) {
        mCCIPaint.setTextSize(textSize);
    }
}
